<script>
import service from '@/utils/request'
import AddOrUpdate from '@/views/generator/databases/add-or-update.vue'

export default {
  name: 'Index',
  components: { AddOrUpdate },
  data() {
    return {
      state: {
        dataListUrl: '/gen/baseclass/page',
        deleteUrl: '/gen/baseclass',
        dataList: [],
        dataListLoading: false,
        queryForm: {
          code: ''
        },
        createdIsNeed: true,
        isPage: true,
        primaryKey: 'id',
        exportUrl: '',
        order: '',
        asc: false,
        page: 1,
        limit: 10,
        total: 0,
        pageSizes: [1, 10, 20, 50, 100, 200],
        dataListSelections: []
      }
    }
  },
  mounted() {
    if (this.state.createdIsNeed) {
      this.query()
    }
  },
  methods: {
    addOrUpdateHandle() {

    },
    query() {
      if (!this.state.dataListUrl) {
        return
      }

      this.state.dataListLoading = true

      service
        .get(this.state.dataListUrl, {
          params: {
            order: this.state.order,
            asc: this.state.asc,
            page: this.state.isPage ? this.state.page : null,
            limit: this.state.isPage ? this.state.limit : null,
            ...this.state.queryForm
          }
        })
        .then((res) => {
          this.state.dataList = this.state.isPage ? res.data.list : res.data
          this.state.total = this.state.isPage ? res.data.total : 0
        })

      this.state.dataListLoading = false
    },
    getDataList() {
      this.state.page = 1
      this.query()
    },
    deleteHandle(key) {
      if (!this.state.deleteUrl) {
        return
      }
      this.$msgbox.confirm('确定进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          service.delete(this.state.deleteUrl + '/' + key).then(() => {
            this.$message.success('删除成功')

            this.query()
          })
        })
        .catch(() => {})
    },
    deleteBatchHandle(key) {
      let data = []
      if (key) {
        data = [key]
      } else {
        data = this.state.dataListSelections ? this.state.dataListSelections : []

        if (data.length === 0) {
          this.$message.warning('请选择删除记录')
          return
        }
      }

      this.$msgbox.confirm('确定进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          if (this.state.deleteUrl) {
            service.delete(this.state.deleteUrl, { data }).then(() => {
              this.$message.success('删除成功')

              this.query()
            })
          }
        })
        .catch(() => {})
    },
    selectionChangeHandle(selections) {
      this.state.dataListSelections = selections.map((item) =>
        this.state.primaryKey && item[this.state.primaryKey])
    },
    sizeChangeHandle(val) {
      this.state.page = 1
      this.state.limit = val
      this.query()
    },
    currentChangeHandle(val) {
      this.state.page = val
      this.query()
    },
    sortChangeHandle(val) {
      const { prop, order } = val

      if (prop && order) {
        this.state.order = prop
        this.state.asc = order === 'ascending'
      } else {
        this.state.order = ''
      }

      this.query()
    }
  }
}
</script>

<template>
  <el-card>
    <el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="state.queryForm.connName" placeholder="连接名" />
      </el-form-item>
      <el-form-item prop="dbType">
        <el-select v-model="state.queryForm.dbType" clearable placeholder="数据库类型">
          <el-option value="MySQL" label="MySQL" />
          <el-option value="Oracle" label="Oracle" />
          <el-option value="PostgreSQL" label="PostgreSQL" />
          <el-option value="SQLServer" label="SQLServer" />
          <el-option value="DM" label="达梦8" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="danger" @click="deleteBatchHandle()">删除</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%" @selection-change="selectionChangeHandle">
      <el-table-column type="selection" header-align="center" align="center" width="50" />
      <el-table-column prop="connName" label="连接名" header-align="center" align="center" />
      <el-table-column prop="dbType" label="数据库类型" header-align="center" align="center" />
      <el-table-column prop="connUrl" label="数据库URL" show-overflow-tooltip header-align="center" align="center" />
      <el-table-column prop="username" label="用户名" header-align="center" align="center" />
      <el-table-column label="密码" header-align="center" align="center">
        <template #default="scope">
          <span>{{ formatPassword(scope.row.password) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="170">
        <template #default="scope">
          <el-button type="primary" link @click="datasourceHandle(scope.row.id)">测试</el-button>
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
          <el-button type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="state.page"
      :page-sizes="state.pageSizes"
      :page-size="state.limit"
      :total="state.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList" />
  </el-card>
</template>

<style scoped lang="scss">

</style>
